package com.zhengshi.formal;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class zhenshi {
//网易基本成型
	@SuppressWarnings("resource")
	public static void main(String[] args) throws IOException {
		//**************唯一更改****************
		String filepath = "I:\\未删选";// 视频文件夹路径I:\未删选
		
		int dengdai = 600;//等待视频上传时间长度
		int katushijian = 20;//视频100%到网易返回图片之间的时间差
		int tanchuAlert = 6;//点击提交按钮后6秒时间内如果不弹出窗口，代表有问题，直接关闭当前页面，防止崩溃
		int buShuaXin = 5; //检测不刷新的次数，页面刷新5次还不出结果，就放行，让浏览器崩溃
		int pageChaoShi = 3;//设置超时时间为3S，页面等待3秒时间还没加载进来就采取行动
		
		final String leimu1 ="电视剧";
		final String leimu2 ="电视剧片段";
		
		String tags1 = "明星";
		String tags2 = "娱乐";
		String tags3 = "综艺";
		String tags4 = "电影";
		String tags5 = "电视剧";
		
		//第几张图片
		String[] dianjitupian = {"//*[@id='auto']/ul/li[2]","//*[@id='auto']/ul/li[3]","//*[@id='auto']/ul/li[4]","//*[@id='auto']/ul/li[5]"
				,"//*[@id='auto']/ul/li[6]","//*[@id='auto']/ul/li[7]","//*[@id='auto']/ul/li[8]","//*[@id='auto']/ul/li[9]"
				,"//*[@id='auto']/ul/li[10]"};
		//**************唯一更改****************
		Random random = new Random();
		String filepathqiege = new String (filepath);
		String zhangHaofilepath = "E:\\ZhangHao";// 帐号文件夹路径
		String filepath1 = filepath.replace("\\", "\\\\");
		File fileExist = new File(filepath + "/a.txt");
		File abExist = new File(filepath + "/ab.txt");
		if (!abExist.exists()) {
			System.out.println("视频没有进行切词动作的禁止上传");
			throw new RuntimeException();
		}
		// 读取视频地址
		InputStreamReader reader1 = new InputStreamReader(new FileInputStream(abExist));
		BufferedReader br1 = new BufferedReader(reader1); 
		String line99 = "";
		line99 = br1.readLine();
		if(line99!=null&&line99.trim()!=null){
			System.out.println("没有解析完的不要来上传，必须所有视频全部解析完");
			throw new RuntimeException();
		}
		
		File zuihouExist = new File(filepath + "/排序.txt");
		if (!zuihouExist.exists()) {
		List<String> paixvLineList = readFile(filepath + "/a.txt", "");
		List<String> zuizhongPaixv = new ArrayList<String>();
		Set<String> myTagbefor1=new HashSet<String>();//第一个首标签
		List<String> dayv6 = new ArrayList<String>();//首关键词次数大于5存进来
		Map<String,Integer> mapPaixv = new HashMap<String,Integer>();
		for (String linepaixv : paixvLineList) {
			String linepaixv1 = linepaixv.split("▲")[0];
			String linepaixvTag = linepaixv1.split("●")[1];
			String shipingjiewei = linepaixv1.split("●")[0];
			if(!shipingjiewei.endsWith(".mp4")||shipingjiewei.split(".mp4")[0].trim().endsWith(")")){
				continue;
			};
			String[] paixvTags = linepaixvTag.split("/");
			List<String> paixvTags3 = new ArrayList<String>();//只要前三个
			for (int i = 0; i < paixvTags.length; i++) {
				if(i==0){
					myTagbefor1.add(paixvTags[i]);//只装载首标签
				}
				paixvTags3.add(paixvTags[i]);
				if(i>=2){
					i=5;
				}
			}
			
			for (String text : paixvTags3) {
				if (mapPaixv.get(text) == null) {
					mapPaixv.put(text, 1);
				} else {
					mapPaixv.put(text, mapPaixv.get(text) + 1);
				}
			}
		}
		mapPaixv.remove("vd");//下载文件为vd的都去除
		// 升序比较器
		Comparator<Map.Entry<String, Integer>> valueComparator = new Comparator<Map.Entry<String, Integer>>() {
			@Override
			public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
				// TODO Auto-generated method stub
				return o2.getValue() - o1.getValue();
			}
		};
		 // map转换成list进行排序
		 List<Map.Entry<String, Integer>> listTag = new ArrayList<Map.Entry<String,Integer>>(mapPaixv.entrySet());
		 // 排序
		 Collections.sort(listTag,valueComparator);
		 for (Entry<String, Integer> entry : listTag) {
			if(entry.getValue()>2){
				dayv6.add(entry.getKey());
			}
		}
		 
		 
		 List<String> listPinglv = new ArrayList<String>();
		 for (Entry<String, Integer> entry : listTag) {
			 System.out.println("key:"+entry.getKey()+"------------value:"+entry.getValue());
			 listPinglv.add("key:"+entry.getKey()+"------------value:"+entry.getValue()+ "\r\n");
		}
		 writeFile(filepath + "/词频.txt", listPinglv);
		 
		 
		 
		 	for (Entry<String, Integer> entry : listTag) {
		 		for (int i = 0; i < paixvLineList.size(); i++) {
		 			String linepaixv =paixvLineList.get(i);
		 			String linepaixv1 = linepaixv.split("▲")[0];
					String linepaixvTag = linepaixv1.split("●")[1];
					String[] paixvTags = linepaixvTag.split("/");
					List<String> tagzhuanSZ = new ArrayList<String>();
					for(int kkk=0;kkk<paixvTags.length;kkk++){//只要三个
						tagzhuanSZ.add(paixvTags[kkk]);
						if (kkk>=2) {
							kkk=paixvTags.length;
						}
					}
					if (tagzhuanSZ.contains(entry.getKey())) {//&&(!dayv6.contains(entry.getKey()))
						paixvLineList.remove(linepaixv);
						zuizhongPaixv.add(linepaixv);
					}
				}
				
			}
		 	
		 	writeFile(filepath + "/排序.txt", zuizhongPaixv);
		}
		
		//**************检测帐号是不是已经全部发送完和视频是不是已经没有了****************
		File fileZhangHao = new File(zhangHaofilepath + "/a.txt"); // 读帐号文件
		InputStreamReader readerZhangHao = new InputStreamReader(new FileInputStream(fileZhangHao)); 
		BufferedReader brZhanghao = new BufferedReader(readerZhangHao); 
		String lineZhanghao = brZhanghao.readLine().trim();
		File shiPingExist = new File(filepath + "/排序.txt");// 读视频文件
		InputStreamReader readerShiPing = new InputStreamReader(new FileInputStream(shiPingExist)); 
		BufferedReader brShiPing = new BufferedReader(readerShiPing); 
		String lineShiPing = brShiPing.readLine().trim();
		if(lineZhanghao==null||lineShiPing==null){
			throw new RuntimeException();
		}
		//**************检测帐号是不是已经全部发送完和视频是不是已经没有了****************
		
		 SimpleDateFormat format0 = new SimpleDateFormat("HH_mm_ss");
		 SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");  
		final WebDriver driver = new ChromeDriver();
		Point targetPosition = new Point(3637,928);
		//driver.manage().window().setPosition(targetPosition );
		driver.manage().window().maximize();
		try {
			for (int bengkui = 0; bengkui < 99999; bengkui++) {
				for (int i = 0; i < buShuaXin; i++) {
						driver.manage().deleteAllCookies();
				        driver.get("http://mp.163.com/#/index");
				        //设置超时时间为3S
				        driver.manage().timeouts().pageLoadTimeout(pageChaoShi, TimeUnit.SECONDS);
				        if(driver.getTitle().contains("登录")){
				        	break;
				        }
				}
				driver.navigate().refresh();
				for (int z = 0; z < 99999; z++) {
				driver.manage().deleteAllCookies();

				SimpleDateFormat sdf = new SimpleDateFormat("M/dd/yyyy hh:mm:ss a",Locale.US);
				
				String name = "NTES_SESS";
				// 读取帐号文件
				File filename2 = new File(zhangHaofilepath + "/a.txt"); // 读帐号文件
				InputStreamReader reader2 = new InputStreamReader(new FileInputStream(filename2)); // 建立一个输入流对象reader
				BufferedReader br2 = new BufferedReader(reader2); // 建立一个对象，它把文件内容转成计算机能读懂的语言
				String line2 = "";
				line2 = br2.readLine().trim();
				List<String> listZhangHaoTip = readFile(zhangHaofilepath + "/a.txt", line2);
				System.out.println("还有【帐号】数量▉"+listZhangHaoTip.size()+"▉没有发送");
				String[] s1 = line2.split("\\/", -1);
				String value = s1[5];
				System.out.println("---------------start---------------------");
				System.out.println(s1[0]+"。开始执行上传任务");
				System.out.println("读入帐号成功：" + value);
				
				String domain = ".mp.163.com";
				String path = "/";
				Date expiry = sdf.parse("5/13/2100 10:31:37 AM");
				Cookie cookie = new Cookie(name, value, domain, path, expiry);
				driver.manage().addCookie(cookie);			
				for (int i = 0; i < buShuaXin; i++) {
				        //设置超时时间为0.3S
						driver.get("http://mp.163.com/index.html#/income-stat");
						//设置超时时间为3S
				        driver.manage().timeouts().pageLoadTimeout(pageChaoShi, TimeUnit.SECONDS);
				        String shouyiTitle = driver.getTitle();
				        if(shouyiTitle.contains("收益统计")){
				        	break;
				        }
				        System.out.println("读收益监测点");
				}
				System.out.println("验证点99999");
				Thread.sleep(200);
				WebElement ddElement1 = driver.findElement(By.xpath("//*[@class='column column-5']/div/ul/li[2]/a"));
				String idString = ddElement1.getAttribute("href").toString().replace("http://mp.163.com/article/postpage/",
						"");
				String videoUrl = "http://mp.163.com/video/postpage/" + idString;
				for (int y = 0; y <= 6; y++) {
					System.out.println("还有【帐号】数量▉("+listZhangHaoTip.size()+"个)▉没有发送");
					
					String iString = "";
					for (int i = 0; i < buShuaXin; i++) {
						driver.get(videoUrl+"?"+random.nextInt(9999));
						//设置超时时间为3S
				        driver.manage().timeouts().pageLoadTimeout(pageChaoShi, TimeUnit.SECONDS);
					    // z-red-font读出次数
						WebElement iDriver = driver.findElement(By.className("z-red-font"));
						if(iDriver!=null&&iDriver.getText().toString()!=null){
							iString = iDriver.getText().toString();
							break;
						}
					}
					
					// 读取视频地址
					File filename1 = new File(filepath + "/排序.txt");
					InputStreamReader reader = new InputStreamReader(new FileInputStream(filename1));
					BufferedReader br = new BufferedReader(reader); 
					String line0 = "";
					line0 = br.readLine().trim();
					String shouzimu = filepathqiege.substring(0,1);
					String zhuanhuan = line0.substring(1);
					String line1 = new String(shouzimu+zhuanhuan);
					String[] line11 = line1.split("●");
					String lineMp4 = line11[0];
					System.out.println("读入视频文件成功：" + lineMp4);
					String yanzheng123 = lineMp4.split(".mp4")[0].trim();
					if (!lineMp4.endsWith(".mp4") || yanzheng123.endsWith(")")) {
						// 删除第一行
						List<String> list = readFile(filepath + "/排序.txt", line0);
						writeFile(filepath + "/排序.txt", list);
						continue;
					}
					
					y = Integer.parseInt(iString);
					System.out.println("正在处理第" + (y + 1) +"个视频");
					if(y>5){
						y=6;	
						for (int i = 0; i < buShuaXin; i++) {
								//设置超时时间为0.3S
								driver.get("http://mp.163.com/index.html#/income-stat");
								//设置超时时间为3S
						        driver.manage().timeouts().pageLoadTimeout(pageChaoShi, TimeUnit.SECONDS);
						        String shouyiTitle = driver.getTitle();
						        if(shouyiTitle.contains("收益统计")){
						        	break;
						        }
						}
						continue;
					}
					driver.findElement(By.id("video-up-input")).sendKeys(lineMp4);
					driver.findElement(By.xpath("//form[@id='videoPost']/div[2]/div[6]/div/ul/li[1]/a/label")).click();
					
					//标题和复制标题开始
					String valueFileTitle99 = driver.findElement(By.id("fileTitle")).getAttribute("value");
					String valueFileTitle=valueFileTitle99.replace("…", "。");
					//----------解决超过27字符和小于11字符---开始
					String valueFileTitleNew = new String(valueFileTitle);
						for (int p = 0; p <= 4; p++) {
							String numberLength = driver.findElement(By.className("j-checkTitle")).getText();
							int intNumberMax = Integer.parseInt(numberLength);
							/*if (intNumberMax > 27) {
								int xz = intNumberMax - 27;
								valueFileTitleNew = valueFileTitleNew.substring(0, valueFileTitleNew.length() - xz);
								driver.findElement(By.id("fileTitle")).clear();
								driver.findElement(By.id("fileTitle")).sendKeys(valueFileTitleNew);
							}else */
								if(intNumberMax < 11){
								valueFileTitleNew=valueFileTitleNew+"。"+valueFileTitleNew;
								driver.findElement(By.id("fileTitle")).clear();
								driver.findElement(By.id("fileTitle")).sendKeys(valueFileTitleNew);
							}else{
								p=5;
								driver.findElement(By.id("fileTitle")).clear();
								driver.findElement(By.id("fileTitle")).sendKeys(valueFileTitleNew);
							}
						}
					
					//----------解决超过27字符和小于11字符---结束

					String[] line12 = line1.split("▲");
					String description = new String(valueFileTitle);
					
					//贴标签开始
						
						if (line12.length > 1) {
							description = line12[1];
							String descriptiontag = line12[0];
							String splitTag = descriptiontag.split("●")[1];
							String[] tags = splitTag.split("/");
							tags1 = tags[0];
							if (tags.length > 1) {
								tags2 = tags[1];
							}
							if (tags.length > 2) {
								tags3 = tags[2];
							}
							if (tags.length > 3) {
								tags4 = tags[3];
							}
							if (tags.length > 4) {
								tags5 = tags[4];
							}
						}
					driver.findElement(By.id("description")).sendKeys(description + "/" + valueFileTitle);
					
					Actions action = new Actions(driver);
					driver.findElement(By.id("form-tags-input")).sendKeys(tags1);
					action.sendKeys(Keys.SPACE).perform();
					driver.findElement(By.id("form-tags-input")).sendKeys(tags2);
					action.sendKeys(Keys.SPACE).perform();
					driver.findElement(By.id("form-tags-input")).sendKeys(tags3);
					action.sendKeys(Keys.SPACE).perform();
					driver.findElement(By.id("form-tags-input")).sendKeys(tags4);
					action.sendKeys(Keys.SPACE).perform();
					driver.findElement(By.id("form-tags-input")).sendKeys(tags5);
					action.sendKeys(Keys.SPACE).perform();
					//贴标签结束
					
					driver.findElement(By.xpath("//*[@id='videoPost']/div[2]/div[4]/div/div[1]/button")).click();
					driver.findElement(By.xpath("//*[@id='videoPost']/div[2]/div[4]/div/div[1]/div/div/input")).sendKeys(leimu1);
					//*[@id="videoPost"]/div[2]/div[4]/div/div[1]/div/ul/li[46]/a/span[1]
					try {
						(new WebDriverWait(driver, 3)).until(new ExpectedCondition<WebElement>() {
							@Override
							public WebElement apply(WebDriver arg0) {
								//*[@id="videoPost"]/div[2]/div[4]/div/div[1]/div/ul/li[46]/a/span[1]
								WebElement suuDriver = driver
										.findElement(By.xpath("//*[@id='videoPost']/div[2]/div[4]/div/div[1]/div/ul/li[46]/a/span[1]"));
									if (suuDriver != null && leimu1.equals(suuDriver.getText().toString())) {
									return suuDriver;
								} else {
									return null;
								}
							}
						});
					} catch (Exception e2) {
						driver.get("http://mp.163.com/index.html#/content");
						Thread.sleep(400);
						driver.switchTo().alert().accept();
						Thread.sleep(5000);
						continue;
					}
					driver.findElement(By.linkText(leimu1)).click();
					action.sendKeys(Keys.ENTER).perform();
					
					//弹起电视剧片段
					driver.findElement(By.xpath("//*[@id='videoPost']/div[2]/div[4]/div/div[2]/button")).click();
					try {
						(new WebDriverWait(driver, 3)).until(new ExpectedCondition<WebElement>() {
							@Override
							public WebElement apply(WebDriver arg0) {
								//*[@id="videoPost"]/div[2]/div[4]/div/div[2]/div/ul/li[1]/a/span[1]
								WebElement suuDriver = driver.findElement(By
										.xpath("//*[@id='videoPost']/div[2]/div[4]/div/div[2]/div/ul/li[1]/a/span[1]"));
								if (suuDriver != null) {
									return suuDriver;
								} else {
									return null;
								}
							}
						});
					} catch (Exception e1) {
						driver.get("http://mp.163.com/index.html#/content");
						Thread.sleep(400);
						driver.switchTo().alert().accept();
						Thread.sleep(5000);
						continue;
					}
					Thread.sleep(100);
					driver.findElement(By.linkText(leimu2)).click();
					Thread.sleep(100);
					/*************突破27字符****************/
					System.out.println("开始突破字数限制");
					JavascriptExecutor js= (JavascriptExecutor) driver;
					//*[@id="videoPost"]/div[2]/div[1]
					WebElement editor = driver.findElement(By.xpath("//*[@id='videoPost']/div[2]/div[1]"));
				    js.executeScript(
				            "arguments[0].setAttribute('class','')",
				            editor);
				    /*************突破27字符***************/
				    Thread.sleep(300);
				    
				    
				    
				    /*************描述中含有违规内容，请修改***************/
				  //*[@id="videoPost"]/div[2]/div[2]/div/p[1]/span
				    WebElement weiguiElement = driver.findElement(By.xpath("//*[@id='videoPost']/div[2]/div[2]/div/p[1]/span"));
				    if ("描述中含有违规内容，请修改".equals(weiguiElement.getText())) {
				    	// 删除第一行
						List<String> list2 = readFile(filepath + "/排序.txt", line0);
						System.out.println("还有("+list2.size()+")个【视频】没有发送");
						writeFile(filepath + "/排序.txt", list2);
						driver.get("http://mp.163.com/index.html#/content");
						Thread.sleep(400);
						driver.switchTo().alert().accept();
						Thread.sleep(5000);
						continue;
					}
				    /*************描述中含有违规内容，请修改***************/
					
				    
				    /*************卡在0.00%不动解决方案***************/
				    WebElement kazhu100 = driver.findElement(By.xpath("//*[@id='videoUp']/div/div[2]/span[2]"));
					if(kazhu100 != null && "0%".equals(kazhu100.getText())){
						driver.get("http://mp.163.com/index.html#/content");
						Thread.sleep(400);
						driver.switchTo().alert().accept();
						Thread.sleep(5000);
						continue;
					}
				    /*************卡在0.00%不动解决方案***************/
					
					
				  //*[@id="videoPost"]/div[2]/div[2]
				    Thread.sleep(50);
				    /*************顶掉弹窗****************/
				    /*int jumpAlert = 0;
					for(int ee =0;ee<9999;ee++){
						System.out.println("检测到没到100");
						WebElement ddElement = driver.findElement(By.className("radio-img"));
						WebElement removeAlert = driver.findElement(By.className("modal-title"));
						if(removeAlert != null && "提示".equals(removeAlert.getText())){
							driver.get("http://mp.163.com/index.html#/content");
							Thread.sleep(400);
							driver.switchTo().alert().accept();
							jumpAlert = 1;
							break;
						}
						if(ddElement != null){
							break;
						}
						Thread.sleep(1000);
					}
					if(jumpAlert ==1){
						continue;
					}*/
				    /*************顶掉弹窗***************/
					System.out.println("弹窗之下");
					
					
					
					//**************等待时间****************
					try {
						WebDriverWait wait = new WebDriverWait(driver, dengdai);
						wait.until(new ExpectedCondition<WebElement>() {
							@Override
							public WebElement apply(WebDriver d) {
								WebElement ddElement = driver.findElement(By.className("percent"));
								if ("100%".equals(ddElement.getText())) {
									return ddElement;
								} else {
									return null;
								}
							}
						});
					} catch (Exception e1) {
						driver.get("http://mp.163.com/index.html#/content");
						Thread.sleep(400);
						driver.switchTo().alert().accept();
						Thread.sleep(5000);
						continue;
					}
					//**************等待时间****************
					
					/*************从100%到卡住图片之间超过自定义秒数，就退出重来***************/
					System.out.println("100%到卡图片之间");
					try {
						(new WebDriverWait(driver, katushijian)).until(new ExpectedCondition<WebElement>() {
							@Override
							public WebElement apply(WebDriver arg0) {
								WebElement kazhu100 = driver.findElement(By.className("radio-img"));
								if (kazhu100 != null) {
									return kazhu100;
								} else {
									return null;
								}
							}
						});
					} catch (Exception e) {
						driver.get("http://mp.163.com/index.html#/content");
						Thread.sleep(400);
						driver.switchTo().alert().accept();
						Thread.sleep(10000);
						continue;
					}
				    /*************从100%到卡住图片之间超过自定义秒数，就退出重来***************/
					
					
					
					//**************解决空白图片被选中****************
					for (int i = 0; i < 99; i++) {
					WebElement tupianClick = driver.findElement(By.xpath(dianjitupian[random.nextInt(9)]));
					WebElement tupianToImg = tupianClick.findElement(By.tagName("input"));
					Thread.sleep(50);
						if(StringUtils.isNotEmpty(tupianToImg.getAttribute("value"))){
							tupianClick.click();
							break;
						}
					}
					//**************解决空白图片被选中****************
					
					System.out.println("视频图片被成功选中");
					
					
				    /*************防止IP探测****************/
					/*JavascriptExecutor jsIp= (JavascriptExecutor) driver;
					//*[@id="inContent"]/input[6]
					WebElement editorIp = driver.findElement(By.xpath("//*[@id='inContent']/input[6]"));
					Random random = new Random();
					String ip = String.valueOf(random.nextInt(124))+"."+String.valueOf(random.nextInt(124))
					+"."+String.valueOf(random.nextInt(255))+"."+String.valueOf(random.nextInt(255));
					jsIp.executeScript(
				            "arguments[0].value=\""+ip+"\"",
				            editorIp);*/
				    /*************防止IP探测***************/
					
					/*************tanchuAlert_秒内弹窗必须出来***************/
					Thread.sleep(500);
					driver.findElement(By.xpath("//*[@id='videoPost']/div[2]/div[7]/button")).click();
					Thread.sleep(300);
					try {
						(new WebDriverWait(driver, tanchuAlert)).until(new ExpectedCondition<WebElement>() {
							@Override
							public WebElement apply(WebDriver arg0) {
								System.out.println("弹出确认框");
								WebElement sDriver = driver.findElement(By.className("js-btn-ok"));
								try {
									Thread.sleep(300);
								} catch (InterruptedException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								if (sDriver != null&&"确定".equals(sDriver.getText().toString())) {
									return sDriver;
								} else {
									return null;
								}
							}
						});
					} catch (Exception e) {
						Thread.sleep(1000);
						driver.get("http://mp.163.com/index.html#/content");
						Thread.sleep(400);
						driver.switchTo().alert().accept();
						Thread.sleep(10000);
						continue;
					}
					/*************tanchuAlert_秒内弹窗必须出来***************/
					
					Thread.sleep(600);
					driver.findElement(By.className("js-btn-ok")).click();
					System.out.println("等待跳过弹窗");
					int breakfor = 0;
					for(int ee =0;ee<9999;ee++){
						
						String title = driver.getTitle();
						System.out.println("监测点9");
						if(!title.contains("内容管理")&&!title.contains("发布视频")){
							driver.get("http://mp.163.com/index.html#/content");
						}else if(title.contains("发布视频")&&ee==3){
							driver.get("http://mp.163.com/index.html#/content");
							Thread.sleep(400);
							driver.switchTo().alert().accept();
							Thread.sleep(10000);
							breakfor = 1;
						}else if(title.contains("内容管理")){
							break;
						}
						Thread.sleep(400);
					}
					if(breakfor ==1){
						continue;
					}
					// 删除第一行
					List<String> listShiPing = readFile(filepath + "/排序.txt", line0);
					System.out.println("还有("+listShiPing.size()+")个【视频】没有发送");
					writeFile(filepath + "/排序.txt", listShiPing);
					if (y >= 5) {
						driver.manage().deleteAllCookies();
						System.out.println("本账号发送给完毕，正在准备发送另外一个账号");
						break;
					}
				}
				// 删除第一行
				List<String> listZhangHao = readFile(zhangHaofilepath + "/a.txt", line2);
				System.out.println("------------------------------------------");
				System.out.println("还有("+listZhangHao.size()+")个【帐号】没有传输完");
				writeFile(zhangHaofilepath + "/a.txt", listZhangHao);
				
				System.out.println(s1[0]+"6个视频成功上传完毕");
				System.out.println("---------------end---------------------");
				}
			}
		} catch (Exception e) {
			File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
		    try {
		    	Date ss = new Date(); 
		    	String time0 = format0.format(ss.getTime());
		    	String time1 = format1.format(ss.getTime());
				FileUtils.copyFile(srcFile, new File("d:\\网易号故障截图\\" + time1 + "\\" + time0 + ".png"));
		    } catch (IOException e1) {
		      e1.printStackTrace();
		    }
		    driver.quit();
		    e.printStackTrace();
		}
		 driver.quit();
	};




	// 方法1：

	public void method1() {
		FileWriter fw = null;
		try {
			// 如果文件存在，则追加内容；如果文件不存在，则创建文件
			File f = new File("E:\\dd.txt");
			fw = new FileWriter(f, true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		PrintWriter pw = new PrintWriter(fw);
		pw.println("追加内容");
		pw.flush();
		try {
			fw.flush();
			pw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 方法2：

	public static void method2(String file, String conent) {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
			out.write(conent + "\r\n");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	// 方法3：

	public static void method3(String fileName, String content) {
		try {
			// 打开一个随机访问文件流，按读写方式
			RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
			// 文件长度，字节数
			long fileLength = randomFile.length();
			// 将写文件指针移到文件尾。
			randomFile.seek(fileLength);
			randomFile.writeBytes(content + "\r\n");
			randomFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static List<String> readFile(String filePath, String str) throws IOException {
		File file = new File(filePath);
		BufferedReader reader = null;
		List<String> list = new ArrayList<String>();
		reader = new BufferedReader(new FileReader(file));
		String text = reader.readLine();
		while (text != null) {
			if (!text.trim().equals(str)) {
				list.add(text + "\r\n");
			}
			text = reader.readLine();
		}
		reader.close();
		return list;
	}

	public static void writeFile(String filePath, List<String> list) throws IOException {
		File file = new File(filePath);
		if (!file.exists()) {
			file.createNewFile();
		}
		FileOutputStream outputStream = new FileOutputStream(file);
		for (String s : list) {
			outputStream.write(s.getBytes());
		}
		outputStream.close();
	}

}
